package stopthread;

/**
 * @description:
 * @author: jhy
 * @time: 2021/4/11 22:56
 */
public class throwExceptionDemo {

    public static void main(String[] args) throws InterruptedException {
        MyThread3 myThread3 = new MyThread3();
        myThread3.start();
        Thread.sleep(200);
        myThread3.interrupt();//中断线程
    }
}
class MyThread3 extends Thread{
    @Override
    public void run() {
        try{
            for(int i = 0;i < 500000;i++){
                if(this.interrupted()){ //判断当前线程，执行当前代码线程的执行状态
                    System.out.println("已经是停止状态了，我要退出了");
                    throw new InterruptedException();
                }
                System.out.println("i =" + (i+1));
            }
            System.out.println("线程抛出异常后进入catch，该行不会被执行");
        }catch (InterruptedException e){
            System.out.println("捕获到线程抛出的异常，将要结束了");
        }
    }
}
